package live.ifox.stage;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import live.ifox.base.BaseRestActivity;
import live.ifox.factory.Entry;
import live.ifox.factory.ID;
import live.ifox.factory.Url;
import live.ifox.helper.AdapterHelper;
import live.ifox.helper.CommonHelper;

public class PositionEditor extends BaseRestActivity {
    boolean updateFlag;
    EditText et_label, et_topic;
    Spinner spinner;
    List<String> topics;
    int _id;
    String _label, _topic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.position_editor);
        actionBar.setTitle(R.string.position);

        updateFlag = false;
        entry = Entry.Position;
        baseUrl = Url.Position;
        spinner = findViewById(R.id.providers);
        et_topic = findViewById(R.id.in_topic);
        et_label = findViewById(R.id.in_label);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                et_topic.setText(topics.get(position));
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {
            }
        });

        _id = getIntent().getIntExtra("id", 0);
        if (_id > 0) {
            updateFlag = true;
            _topic = getIntent().getStringExtra("topic");
            _label = getIntent().getStringExtra("label");
            et_label.setText(_label);
        }
        updateCategory();
    }

    public void onClean_Click(View view) {
        if (!et_topic.getText().toString().isEmpty())
            et_topic.setText("");

        if (!et_label.getText().toString().isEmpty())
            et_label.setText("");
    }

    void updateCategory() {
        httpGet(baseUrl + "get_topics");
    }

    public void onSubmit_Click(View view) {
        String topic = et_topic.getText().toString();
        String label = et_label.getText().toString();
        if (label.isEmpty() || topic.isEmpty()) {
            CommonHelper.showMessage(this, "名称、货位不能为空！");
            return;
        }
        Map<String, Object> map = new HashMap<>();
        if (!updateFlag) {
            map.put("topic", topic);
            map.put("label", label);
            httpPost(baseUrl + "insert", JSON.toJSONString(map));
        } else {
            if (!_topic.equals(topic))
                map.put("topic", topic);

            if (!_label.equals(label))
                map.put("label", label);

            if (map.isEmpty())
                return;

            map.put("id", _id);
            httpPost(baseUrl + "update", JSON.toJSONString(map));
            setResult(ID.RESULT_DONE);
        }
        finish();
    }

    @Override
    public void processReply(JSONObject rootObject) {
        JSONArray array = AdapterHelper.verifyObject(rootObject,
                "entry", entry, "topics");
        if (array.isEmpty())
            return;

        topics = array.toJavaList(String.class);
        ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
                android.R.layout.simple_spinner_dropdown_item);
        adapter.addAll(topics);
        spinner.setAdapter(adapter);
        if (updateFlag) {
            if (topics.contains(_topic))
                spinner.setSelection(topics.indexOf(_topic));
        }
    }
}
